//
//  UserViewController.m
//  CloudWeibo
//
//  Created by CLOUD on 1/15/15.
//  Copyright (c) 2015 Cloud. All rights reserved.
//

#import "UserViewController.h"
#import "UserInfoView.h"
#import "UIFactory.h"
#import "UserModel.h"
#import "WeiBoModel.h"

@interface UserViewController ()

@end

@implementation UserViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    _requests = [[NSMutableArray alloc] init];
    self.title = @"个人资料";
    self.userInfoView = [[[UserInfoView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)] autorelease];
    UIButton *homeButton = [UIFactory createButton:@"tabbar_home.png" highlited:@"tabbar_home_highlighted.png"];
    homeButton.frame = CGRectMake(0, 0, 34, 72);
    UIBarButtonItem *homeItem = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
    [homeButton addTarget:self action:@selector(goHome) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [homeItem autorelease];
    
    self.tableView.eventDelegate = self;
    [self loadUserData];
    [self loadWeiBoData];
}

- (void)goHome
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)loadUserData
{
    if (self.userName == nil) {
        NSLog(@"error:用户为空");
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:self.userName forKey:@"screen_name"];
    SinaWeiboRequest *request = [self.sinaweibo requestWithURL:@"users/show.json" params:params         httpMethod:@"GET" block:^(id result){
        [self loadUserDataFinish:result];
    }];
    [_requests addObject:request];
}

- (void)loadUserDataFinish:(id)result
{
    UserModel *user = [[UserModel alloc] initWithDataDic:result];
    self.userInfoView.user = user;
    [self refreshUI];
}

- (void)loadWeiBoData
{
    if (self.userName == nil) {
        NSLog(@"error:用户为空");
        return;
    }
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObject:self.userName forKey:@"screen_name"];
    SinaWeiboRequest *request = [self.sinaweibo requestWithURL:@"statuses/user_timeline.json" params:params httpMethod:@"GET" block:^(id result){
        [self loadWeiBoDataDataFinish:result];
    }];
    [_requests addObject:request];
}

- (void)loadWeiBoDataDataFinish:(id)result
{
    NSArray *statues = [result objectForKey:@"statuses"];
    NSMutableArray *weiBos = [NSMutableArray arrayWithCapacity:statues.count];
    for (NSDictionary *dic in statues) {
        WeiBoModel *weiBoModel = [[WeiBoModel alloc] initWithDataDic:dic];
        [weiBos addObject:weiBoModel];
        [weiBoModel release];
    }
    
    self.tableView.data = weiBos;
    
    if (weiBos.count >= 20) {
        self.tableView.isMore = YES;
    }else{
        self.tableView.isMore = NO;
    }
    [self.tableView reloadData];
}

- (void) pullDown:(BaseTableView *) tableView
{
    [tableView performSelector:@selector(doneLoadingTableViewData) withObject:nil afterDelay:2];
}

- (void) pullUp:(BaseTableView *)tableView
{
    [tableView performSelector:@selector(stopPullup) withObject:nil afterDelay:2];
}


- (void)refreshUI
{
    [super showLoading:NO];
    self.tableView.hidden = NO;
    self.tableView.tableHeaderView = _userInfoView;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    for (SinaWeiboRequest *request in _requests) {
        [request disconnect];
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
@end
